package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.Message;
import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.MessageActionForm;
import com.foreveredu.propms.web.struts.actionform.NewsActionForm;
import com.foreveredu.propms.web.struts.actionform.OwnerActionForm;

public class MessageAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 通过ID查找news详情
		public ActionForward toAdd(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			return mapping.findForward("toAdd");
		}
	// 通过ID查找news详情
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageActionForm maf = (MessageActionForm) form;
		// 表单校验、
		if (maf.getTopic() == null || "".equals(maf.getTopic())) {
			request.setAttribute("messageTopicError", "请输入主题");

			return mapping.findForward("toAdd");
		}
		if (maf.getName() == null || "".equals(maf.getName())) {
			request.setAttribute("messageNameError", "请输入姓名");

			return mapping.findForward("toAdd");
		}
		if (maf.getPhone() == null || "".equals(maf.getPhone())) {
			request.setAttribute("messagePhoneError", "请输入联系方式");

			return mapping.findForward("toAdd");
		}
		if (maf.getEmail() == null || "".equals(maf.getEmail())) {
			request.setAttribute("messageEmailError", "请输入邮件地址");

			return mapping.findForward("toAdd");
		}
		if (maf.getContent() == null || "".equals(maf.getContent())) {
			request.setAttribute("messageContentError", "请输入留言内容");

			return mapping.findForward("toAdd");
		}
		
		Message message = new Message();
		PropertyUtils.copyProperties(message, maf);

		managementService.saveMessage(message);
		return mapping.findForward("toMessageList");
	}
	
	public ActionForward messageList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Message> messageList= managementService.findMessageListForTen();
		request.setAttribute("messageList", messageList);
		return mapping.findForward("messageList");
	}
	
	public ActionForward findMessageById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageActionForm messageform = (MessageActionForm) form;
		Message message = managementService.findMessageById(messageform.getId());
		request.setAttribute("message", message);
		return mapping.findForward("findMessageById");
	}
	
/*
	//News 翻页列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if(offset < 0){
			return mapping.findForward("newsList");
		}
		PageModel pm = managementService.findNewsList(offset ,newsform.getBelong());
		request.setAttribute("pm", pm);
		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsList");
		}	
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgList");
		}
		return null;
	}*/
}
